package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description NursingProjectServiceImpl
 * @Author zhangruoxin
 * @Date 2024-10-15
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页查询护理项目列表
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        // 1.定义PageHelper的页码和每页大小
        PageHelper.startPage(pageNum,pageSize);

        // 2.执行分页查询数据返回Page<NursingProjectVo>
        Page<NursingProjectVo> page =nursingProjectMapper.getByPage(name,status);

        // 3.封装给PageResponse<NursingProjectVo>
        PageResponse<NursingProjectVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult()); //封装当前页数据列表

        // 4.返回数据
        return pageResponse;
    }

    /**
     * 新增护理项目
     * @param nursingProjectDto
     */
    public void add(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.add(nursingProject);
    }

    /**
     * 根据id查询护理项目
     * @param id
     * @return
     */
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject=nursingProjectMapper.getById(id);
        NursingProjectVo nursingProjectVo = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        return nursingProjectVo;
    }

    /**
     * 修改护理项目
     * @param nursingProjectDto
     */
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 启用禁用
     * @param id
     * @param status
     */
    public void updateStatus(Long id, Integer status) {
        NursingProject nursingProject=new NursingProject();
        nursingProject.setId(id);
        nursingProject.setStatus(status);
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 删除护理项目
     * @param id
     */
    public void delete(Long id) {
        nursingProjectMapper.deleteById(id);
    }
}
